package com.LeeCode;

/**
 * 使数组等于目标数组所需的最少操作次数
 */

public class Code3229 {
    public static void main(String[] args) {
        int[] nums = {9, 2, 6, 10, 4, 8, 3, 4, 2, 3}, target = {9, 5, 5, 1, 7, 9, 8, 7, 6, 5};

        System.out.println(new Code3229().minimumOperations(nums, target));
    }

    public long minimumOperations(int[] nums, int[] target) {
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            nums[i] = nums[i] - target[i];
        }

        long posSum = 0, negSum = 0;
        int prev = 0;
        for (int num : nums) {
            int temp = num - prev;
            if (temp > 0) {
                posSum += temp;
            } else {
                negSum -= temp;
            }
            prev = num;
        }

        return Math.max(posSum, negSum);
    }
}
